package org.example.interceptor;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.model.Constant;
import org.example.utils.JwtUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1. 获取token
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);
        log.info("从header中获取token: {}", token);
        // 2. 校验token，判断是否放行
        Claims claims = JwtUtils.parseJwt(token);
        if (claims == null) {
            // 校验失败
            response.setStatus(401);
            log.info("bbb");
            return false;
        }
        log.info("aaa");
        return true;
    }
}
